import requests, json,random
from PyQt5.QtCore import QThread, pyqtSignal

def aqu_url(cost, code, name):
    with open(".\\dip\\rqs_new.json", mode='r', encoding="utf8") as f:
            json_result = json.load(f)
    
    rqs = json_result
    rqs["data"]["medical_case"]["admission_disease_id"] = code
    rqs["data"]["medical_case"]["admission_disease_name"] = name
    rqs["data"]["medical_case"]["discharge_disease_id"] = code
    rqs["data"]["medical_case"]["discharge_disease_name"] = name
    rqs["data"]["medical_case"]["total_fee"] = cost
    rqs["data"]["medical_case"]["self_fee"] = cost
    rqs["data"]["medical_case"]["total_amount"] = cost

    x ="".join(random.choices("赵钱孙李周武郑陈刘王张胡", k=1))
    m ="".join(random.choices("萱明霖玲铵菁梦岚苑婕馨瑗琰乐佳涵晋逸沅海圣姬", k=1))
    
    rqs["data"]["medical_case"]["patient_name"] = x+m
    rqs_url = "http://10.68.60.67:82/partner"
    users = requests.Session()
    headers = {
        "User-Agent":"Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36",
    }
    search_result = users.post(
        rqs_url, 
        headers=headers,
        json=rqs

    )
    result = search_result.json()
    print(result)
    result = result.get("result")
    url = "无记录"
    if len(result)>1:
        url = result[1].get("result")
  
        token = url.split("=")[1]
        suggest_url = "http://10.68.60.67:9931/api/partner/suggest"
        suggest_result = users.post(
            suggest_url, 
            headers=headers,
            data={"token": token}

        ).json()
        # with open('dip.json', 'w', encoding='utf-8') as f:
        #     json.dump(suggest_result, f, ensure_ascii=False)
        return suggest_result, url


    
class DipThread(QThread):
    success = pyqtSignal(dict, str)
    error = pyqtSignal(str)

    def __init__(self, code, name, cost, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.code = code
        self.name = name
        self.cost = cost


    def run(self):

        try:
            
            result, url = aqu_url(self.cost, self.code, self.name)
            
            self.success.emit(result, url)

       


        except Exception as e:
            
            self.error.emit(f"出错：{e}")
if __name__ == '__main__':
    print("url", aqu_url("5000", "J18.900", "肺炎"))